import { baseRequestClient, requestClient } from '#/api/request';

export namespace SubSettlementApi {
  /** 子结算单列表查询参数 */
  export interface SubSettlementListParams {
    page?: number;
    per_page?: number;
    settlement_id?: string;
    status?: string;
    name?: string;
    phone?: string;
    idCard?: string;
    freelancer_keyword?: string;
    freelancer_id?: number;
    sub_settlement_id?: string;
    start_date?: string;
    end_date?: string;
    sort_by?: string;
    sort_order?: 'asc' | 'desc';
  }

  /** 子结算单记录接口 */
  export interface SubSettlementRecord {
    id: string;
    sub_settlement_number: string;
    settlement_id: string;
    freelancer_id: number;
    name: string;
    phone: string;
    idCard: string;
    bankAccount?: string;
    bankName?: string;
    commission: number;
    serviceFee: number;
    serviceFeeRate: number;
    tax: number;
    taxAmount?: number;
    requiredBalance?: number;
    required_balance?: number;
    preSettlementAmount: number;
    status: string;
    statusDisplay: string;
    statusDesc?: string;
    paymentRemarks?: string;
    electronicReceipt?: any;
    releaseTime?: string;
    createdAt: string;
    updatedAt?: string;
    verifiedAt?: string;
    submittedAt?: string;
    distributeStartedAt?: string;
    cancelledAt?: string;
    canEdit: boolean;
    canCancel: boolean;
    canOfflineAuth: boolean;
    settlement?: {
      id: string;
      settlement_name: string;
      settlement_number: string;
    };
    freelancer?: {
      id: number;
      name: string;
      phone: string;
      email?: string;
    };
  }

  /** 分页响应结构 */
  export interface SubSettlementListResponse {
    data: SubSettlementRecord[];
    current_page: number;
    per_page: number;
    total: number;
    last_page: number;
    from?: number;
    to?: number;
    first_page_url?: string;
    last_page_url?: string;
    next_page_url?: string;
    prev_page_url?: string;
    path?: string;
    links?: any[];
  }

  /** API响应包装 */
  export interface ApiResponse {
    success: boolean;
    data: any;
    message?: string;
  }

  /** 操作参数 */
  export interface VerifyParams {
    action: 'pass' | 'fail';
    remarks?: string;
  }

  export interface DistributeParams {
    action: 'start' | 'complete' | 'fail';
    electronic_receipt?: any;
    remarks?: string;
  }

  export interface BatchActionParams {
    action: 'verify_pass' | 'verify_fail' | 'submit' | 'cancel' | 'start_distribute';
    ids: string[];
    remarks?: string;
  }
}

/**
 * 获取子结算单列表
 */
export async function getSubSettlementListApi(params: SubSettlementApi.SubSettlementListParams) {
  return requestClient.get<SubSettlementApi.SubSettlementListResponse>('/api/tenant/sub-settlements', {
    params,
  });
}

/**
 * 获取子结算单详情
 */
export async function getSubSettlementDetailApi(id: string) {
  return requestClient.get<SubSettlementApi.ApiResponse<SubSettlementApi.SubSettlementRecord>>(`/api/tenant/sub-settlements/${id}`);
}

/**
 * 创建子结算单
 */
export async function createSubSettlementApi(data: Partial<SubSettlementApi.SubSettlementRecord>) {
  return requestClient.post<SubSettlementApi.ApiResponse<SubSettlementApi.SubSettlementRecord>>('/api/tenant/sub-settlements', data);
}

/**
 * 更新子结算单
 */
export async function updateSubSettlementApi(
  id: string,
  data: Partial<SubSettlementApi.SubSettlementRecord>,
) {
  return requestClient.put<SubSettlementApi.ApiResponse<SubSettlementApi.SubSettlementRecord>>(
    `/api/tenant/sub-settlements/${id}`,
    data,
  );
}

/**
 * 删除子结算单
 */
export async function deleteSubSettlementApi(id: string) {
  return requestClient.delete<SubSettlementApi.ApiResponse<null>>(`/api/tenant/sub-settlements/${id}`);
}

/**
 * 验证子结算单
 */
export async function verifySubSettlementApi(id: string, data: SubSettlementApi.VerifyParams) {
  return requestClient.post<SubSettlementApi.ApiResponse<SubSettlementApi.SubSettlementRecord>>(`/api/tenant/sub-settlements/${id}/verify`, data);
}

/**
 * 提交子结算单
 */
export async function submitSubSettlementApi(id: string) {
  return requestClient.post<SubSettlementApi.ApiResponse<SubSettlementApi.SubSettlementRecord>>(`/api/tenant/sub-settlements/${id}/submit`);
}

/**
 * 发放操作
 */
export async function distributeSubSettlementApi(id: string, data: SubSettlementApi.DistributeParams) {
  return requestClient.post<SubSettlementApi.ApiResponse<SubSettlementApi.SubSettlementRecord>>(`/api/tenant/sub-settlements/${id}/distribute`, data);
}

/**
 * 取消子结算单
 */
export async function cancelSubSettlementApi(id: string, data: { remarks?: string }) {
  return requestClient.post<SubSettlementApi.ApiResponse<SubSettlementApi.SubSettlementRecord>>(`/api/tenant/sub-settlements/${id}/cancel`, data);
}

/**
 * 退票操作
 */
export async function refundSubSettlementApi(id: string, data: { remarks: string }) {
  return requestClient.post<SubSettlementApi.ApiResponse<SubSettlementApi.SubSettlementRecord>>(`/api/tenant/sub-settlements/${id}/refund`, data);
}

/**
 * 批量操作
 */
export async function batchActionSubSettlementApi(data: SubSettlementApi.BatchActionParams) {
  return requestClient.post<SubSettlementApi.ApiResponse<null>>('/api/tenant/sub-settlements/batch-action', data);
}

/**
 * 获取状态选项
 */
export async function getSubSettlementStatusOptionsApi() {
  return requestClient.get<SubSettlementApi.ApiResponse<Record<string, string>>>('/api/tenant/sub-settlements/status-options');
}

/**
 * 获取统计信息
 */
export async function getSubSettlementStatisticsApi(params?: { settlement_id?: string }) {
  return requestClient.get<SubSettlementApi.ApiResponse<any>>('/api/tenant/sub-settlements/statistics', { params });
}
